﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class HistoryBillSummaryObject
    {
        public static HistoryBillSummary[] GetHistoryBillSummaries()
        {
            return Query(null);
        }

        public static HistoryBillSummary GetHistoryBillSummary(int id)
        {
            HistoryBillSummary historyBillSummary = new HistoryBillSummary();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(HistoryBillSummaryDesc.BillId, ConditionExpress.EQUALS, id);
            builder.QueryTableName = HistoryBillSummaryDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable historyBillSummaries = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            historyBillSummary.BillId                     = Convert.ToInt32(historyBillSummaries.Rows[0][HistoryBillSummaryDesc.BillId]);
            historyBillSummary.tableName                  = Convert.ToString(historyBillSummaries.Rows[0][HistoryBillSummaryDesc.tableName]);
            historyBillSummary.TableProperty              = Convert.ToString(historyBillSummaries.Rows[0][HistoryBillSummaryDesc.TableProperty]);
            historyBillSummary.TableCategory              = Convert.ToString(historyBillSummaries.Rows[0][HistoryBillSummaryDesc.TableCategory]);
            historyBillSummary.BillTime                   = Convert.ToDateTime(historyBillSummaries.Rows[0][HistoryBillSummaryDesc.BillTime]);
            historyBillSummary.HeadCount                  = Convert.ToInt32(historyBillSummaries.Rows[0][HistoryBillSummaryDesc.HeadCount]);
            historyBillSummary.TotalPrice                 = Convert.ToSingle(historyBillSummaries.Rows[0][HistoryBillSummaryDesc.TotalPrice]);
            historyBillSummary.discount                   = Convert.ToInt32(historyBillSummaries.Rows[0][HistoryBillSummaryDesc.discount]);
            historyBillSummary.Pay                        = Convert.ToSingle(historyBillSummaries.Rows[0][HistoryBillSummaryDesc.Pay]);
            historyBillSummary.Waiter                     = Convert.ToString(historyBillSummaries.Rows[0][HistoryBillSummaryDesc.Waiter]);
            historyBillSummary.ServiceTips                = Convert.ToSingle(historyBillSummaries.Rows[0][HistoryBillSummaryDesc.ServiceTips]);
            historyBillSummary.CheckOutMode               = Convert.ToString(historyBillSummaries.Rows[0][HistoryBillSummaryDesc.CheckOutMode]);

           

            return historyBillSummary;
        }

        public static HistoryBillSummary[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + HistoryBillSummaryDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                HistoryBillSummary[] historyBillSummaries = new HistoryBillSummary[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    HistoryBillSummary historyBillSummary = new HistoryBillSummary();

                    historyBillSummary.BillId               = Convert.ToInt32(table.Rows[i][HistoryBillSummaryDesc.BillId]);
                    historyBillSummary.tableName            = Convert.ToString(table.Rows[i][HistoryBillSummaryDesc.tableName]);
                    historyBillSummary.TableProperty        = Convert.ToString(table.Rows[i][HistoryBillSummaryDesc.TableProperty]);
                    historyBillSummary.TableCategory        = Convert.ToString(table.Rows[i][HistoryBillSummaryDesc.TableCategory]);
                    historyBillSummary.BillTime             = Convert.ToDateTime(table.Rows[i][HistoryBillSummaryDesc.BillTime]);
                    historyBillSummary.HeadCount            = Convert.ToInt32(table.Rows[i][HistoryBillSummaryDesc.HeadCount]);
                    historyBillSummary.TotalPrice           = Convert.ToSingle(table.Rows[i][HistoryBillSummaryDesc.TotalPrice]);
                    historyBillSummary.discount             = Convert.ToInt32(table.Rows[i][HistoryBillSummaryDesc.discount]);
                    historyBillSummary.Pay                  = Convert.ToSingle(table.Rows[i][HistoryBillSummaryDesc.Pay]);
                    historyBillSummary.Waiter               = Convert.ToString(table.Rows[i][HistoryBillSummaryDesc.Waiter]);
                    historyBillSummary.ServiceTips          = Convert.ToSingle(table.Rows[i][HistoryBillSummaryDesc.ServiceTips]);
                    historyBillSummary.CheckOutMode         = Convert.ToString(table.Rows[i][HistoryBillSummaryDesc.CheckOutMode]);


                    historyBillSummaries.SetValue(historyBillSummary, i);
                }

                return historyBillSummaries;
            }
            return null;
        }

        public static Int32 Add(HistoryBillSummary historyBillSummary)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(HistoryBillSummaryDesc.BillId,                historyBillSummary.BillId);
            builder.InsertAdd(HistoryBillSummaryDesc.tableName,             historyBillSummary.tableName);
            builder.InsertAdd(HistoryBillSummaryDesc.TableProperty,         historyBillSummary.TableProperty);
            builder.InsertAdd(HistoryBillSummaryDesc.TableCategory,         historyBillSummary.TableCategory);
            builder.InsertAdd(HistoryBillSummaryDesc.BillTime,              historyBillSummary.BillTime);
            builder.InsertAdd(HistoryBillSummaryDesc.HeadCount,             historyBillSummary.HeadCount);
            builder.InsertAdd(HistoryBillSummaryDesc.TotalPrice,            historyBillSummary.TotalPrice);
            builder.InsertAdd(HistoryBillSummaryDesc.discount,              historyBillSummary.discount);
            builder.InsertAdd(HistoryBillSummaryDesc.Pay,                   historyBillSummary.Pay);
            builder.InsertAdd(HistoryBillSummaryDesc.Waiter,                historyBillSummary.Waiter);
            builder.InsertAdd(HistoryBillSummaryDesc.ServiceTips,           historyBillSummary.ServiceTips);
            builder.InsertAdd(HistoryBillSummaryDesc.CheckOutMode,          historyBillSummary.CheckOutMode);

            builder.QueryTableName = HistoryBillSummaryDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(HistoryBillSummaryDesc.BillId, ConditionExpress.EQUALS, id);
            builder.QueryTableName = HistoryBillSummaryDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(HistoryBillSummary historyBillSummary)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(HistoryBillSummaryDesc.BillId,              historyBillSummary.BillId);
            builder.UpdateAdd(HistoryBillSummaryDesc.tableName,           historyBillSummary.tableName);
            builder.UpdateAdd(HistoryBillSummaryDesc.TableProperty,       historyBillSummary.TableProperty);
            builder.UpdateAdd(HistoryBillSummaryDesc.TableCategory,       historyBillSummary.TableCategory);
            builder.UpdateAdd(HistoryBillSummaryDesc.BillTime,            historyBillSummary.BillTime);
            builder.UpdateAdd(HistoryBillSummaryDesc.HeadCount,           historyBillSummary.HeadCount);
            builder.UpdateAdd(HistoryBillSummaryDesc.TotalPrice,          historyBillSummary.TotalPrice);
            builder.UpdateAdd(HistoryBillSummaryDesc.discount,            historyBillSummary.discount);
            builder.UpdateAdd(HistoryBillSummaryDesc.Pay,                 historyBillSummary.Pay);
            builder.UpdateAdd(HistoryBillSummaryDesc.Waiter,              historyBillSummary.Waiter);
            builder.UpdateAdd(HistoryBillSummaryDesc.ServiceTips,         historyBillSummary.ServiceTips);
            builder.UpdateAdd(HistoryBillSummaryDesc.CheckOutMode,        historyBillSummary.CheckOutMode);

            builder.ConditionAdd(HistoryBillSummaryDesc.BillId, ConditionExpress.EQUALS, historyBillSummary.BillId);
            builder.QueryTableName = HistoryBillSummaryDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
